package com.ruoyi.web.controller.caipiao;

import com.ruoyi.caipiao.service.ICaipiaoPl5MethodOneService;
import com.ruoyi.caipiao.utils.Pl3NumUtil;
import com.ruoyi.caipiao.utils.Pl5NumUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author wangcen
 * @date 2025/6/7 21:20
 */
@RestController
@RequestMapping("caipiao/common")
public class CaipPiaoCommonController extends BaseController {
    @Autowired
    private ICaipiaoPl5MethodOneService caipiaoPl5MethodOneService;

    @PostMapping("calNums/{ty}")
    public AjaxResult calNums(@PathVariable("ty") Integer ty, @RequestBody List<String> list) {
        if (ty == 1) {
            String s1 = list.get(0);
            Set<String> set1 = Pl3NumUtil.str2Set(s1);

            for (int i = 1; i < list.size(); i++) {
                if (StringUtils.isNotBlank(list.get(i))) {
                    set1.retainAll(Pl5NumUtil.str2Set(list.get(i)));
                }
            }
            logger.info("生成号码:{}" , String.join("," , set1));
            return success(String.join("," , set1)).put("size" , set1.size());
        } else if (ty == 2) {
            String s1 = list.get(0);
            Set<String> set1 = Pl3NumUtil.str2Set(s1);

            for (int i = 1; i < list.size(); i++) {
                if (StringUtils.isNotBlank(list.get(i))) {
                    set1.addAll(Pl5NumUtil.str2Set(list.get(i)));
                }
            }
            logger.info("生成号码:{}" , String.join("," , set1));
            return success(String.join("," , set1)).put("size" , set1.size());
        } else {
            Set<String> set1 = Pl3NumUtil.getFj(Pl3NumUtil.str2Set(list.get(0)));
            return success(String.join("," , set1)).put("size" , set1.size());
        }
    }

    @PostMapping("cal/{ty}")
    public AjaxResult cal(@PathVariable("ty") Integer ty, @RequestBody List<String> list) {
        if (ty == 1) { //交集
            if (list.get(0).contains(";")) {
                String[] split = list.get(0).split(";");
                Set<String> set = Pl3NumUtil.positionNum2Code(split[0], split[1], split[2]);
                for (int i = 1; i < list.size(); i++) {
                    String[] split1 = list.get(i).split(";");
                    set.retainAll(Pl3NumUtil.positionNum2Code(split1[0], split1[1], split1[2]));
                }
                logger.info("生成号码:{}" , String.join("," , set));
                return success(String.join("," , set)).put("size" , set.size());
            } else {
                Set<String> set = Pl3NumUtil.str2Set(list.get(0));
                for (int i = 1; i < list.size(); i++) {
                    set.retainAll(Pl3NumUtil.str2Set(list.get(i)));
                }
                logger.info("生成号码:{}" , String.join("," , set));
                return success(String.join("," , set)).put("size" , set.size());
            }
        } else if (ty == 2) {
            if (list.get(0).contains(";")) {
                String[] split = list.get(0).split(";");
                Set<String> set = Pl3NumUtil.positionNum2Code(split[0], split[1], split[2]);
                for (int i = 1; i < list.size(); i++) {
                    String[] split1 = list.get(i).split(";");
                    set.addAll(Pl3NumUtil.positionNum2Code(split1[0], split1[1], split1[2]));
                }
                logger.info("生成号码:{}" , String.join("," , set));
                return success(String.join("," , set)).put("size" , set.size());
            } else {
                Set<String> set = Pl3NumUtil.str2Set(list.get(0));
                for (int i = 1; i < list.size(); i++) {
                    set.addAll(Pl3NumUtil.str2Set(list.get(i)));
                }
                logger.info("生成号码:{}" , String.join("," , set));
                return success(String.join("," , set)).put("size" , set.size());
            }
        } else {
            Set<String> set = Pl3NumUtil.str2Set(list.get(0));
            set = Pl3NumUtil.getFj(set);
            logger.info("生成号码:{}" , String.join("," , set));
            return success(String.join("," , set)).put("size" , set.size());
        }
    }

    @PostMapping("getNums")
    public AjaxResult getNums(@RequestBody List<String> list) {
        if (list.size() == 3) {
            Set<String> set = Pl3NumUtil.positionNum2Code(list.get(0), list.get(1), list.get(2));
            logger.info("生成号码:{}" , String.join("," , set));
            return success(String.join("," , set)).put("size" , set.size());
        } else {
            Set<String> set = Pl5NumUtil.positionNum2Code(list.get(0), list.get(1), list.get(2), list.get(3), list.get(4));
            logger.info("生成号码:{}" , String.join("," , set));
            return success(String.join("," , set)).put("size" , set.size());
        }
    }

}
